/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package conn;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author jm1156
 */
public class DatabaseConnection {

  private Connection connection;
  private String driverName;
  private String databaseName;
  private String serverName;
  private String portNumber;
  private String username;
  private String password;

  public DatabaseConnection(String driverName, String serverName, String portNumber, String username, String databaseName, String password) {
    this.driverName = driverName;
    this.databaseName = databaseName;
    this.serverName = serverName;
    this.portNumber = portNumber;
    this.username = username;
    this.password = password;
  }

  public String getDatabaseName() {
    return databaseName;
  }

  public String getDriverName() {
    return driverName;
  }

  public String getPassword() {
    return password;
  }

  public String getPortNumber() {
    return portNumber;
  }

  public String getServerName() {
    return serverName;
  }

  public String getUsername() {
    return username;
  }

  public void setDatabaseName(String databaseName) {
    this.databaseName = databaseName;
  }

  public void setDriverName(String driverName) {
    this.driverName = driverName;
  }

  public void setPassword(String password) {
    this.password = password;
  }

  public void setPortNumber(String portNumber) {
    this.portNumber = portNumber;
  }

  public void setServerName(String serverName) {
    this.serverName = serverName;
  }

  public void setUsername(String username) {
    this.username = username;
  }

  public boolean connect() {
    try {
      // only connect if connection has expired
      if (connection == null || !connection.isValid(5)) {
        Class.forName("com.mysql.jdbc.Driver");
        String strConnection = "jdbc:" + this.driverName
                + "://" + this.serverName
                + ":" + this.portNumber
                + "/" + this.databaseName;

        System.out.println("Connection: " + strConnection);
        //System.out.println("Username: " + getUsername());
        //System.out.println("Password: " + getPassword());
        connection = DriverManager.getConnection(
                //"jdbc:mysql://66.197.136.195/emisredb_untrusted",
                strConnection,
                username,
                password);
      }
      return connection != null;

    } catch (ClassNotFoundException ex) {
      Logger.getLogger(DatabaseConnection.class.getName()).log(Level.SEVERE, null, ex);
      return false;
    } catch (SQLException ex) {
      Logger.getLogger(DatabaseConnection.class.getName()).log(Level.SEVERE, null, ex);
      return false;
    }
  }

  public Connection getConnection() {
    return connection;
  }
}
